package com.imon.meere.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

@Component
public class VehicleDaoImpl implements VehicleDao {
	private static final Log LOG = LogFactory.getLog(VehicleDaoImpl.class);

	@Resource
	private DataSource dataSource;

	private Connection connection;

	public VehicleDaoImpl() {
		dataSource = null;
		connection = null;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(final DataSource aDataSource) {
		this.dataSource = aDataSource;
	}

	@Override
	public void delete(final Vehicle vehicle) {
		// TODO Auto-generated method stub

	}

	@Override
	public Vehicle findByVehicleNo(final String vehicleNo) {
		final String sql = "SELECT * FROM VEHICLE WHERE VEHICLE_NO = ?";

		try {
			if (connection == null) {
				connection = dataSource.getConnection();
			}
			final PreparedStatement preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, vehicleNo);
			final ResultSet resultSet = preparedStatement.executeQuery();
			Vehicle vehicle = null;
			if (resultSet.next()) {
				vehicle = new Vehicle(resultSet.getString("VEHICLE_NO"), resultSet.getString("COLOR"),
						resultSet.getInt("WHEEL"), resultSet.getInt("SEAT"));
			}
			resultSet.close();
			preparedStatement.close();

			return vehicle;
		} catch (final SQLException e) {
			LOG.error("Failed to run sql query", e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public void insert(final Vehicle vehicle) {
		final String sql = "INSERT INTO VEHICLE (VEHICLE_NO, COLOR, WHEEL, SEAT) " + "VALUES (?, ?, ?, ?)";

		try {
			if (connection == null)
				connection = dataSource.getConnection();

			final PreparedStatement prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setString(1, vehicle.getVehicleNo());
			prepareStatement.setString(2, vehicle.getColor());
			prepareStatement.setInt(3, vehicle.getWheel());
			prepareStatement.setInt(4, vehicle.getSeat());
			prepareStatement.execute();
			prepareStatement.close();
		} catch (final SQLException e) {
			LOG.error("Error executing SQL query", e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public void update(final Vehicle vehicle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteAll() {
		final String sql = "DELETE FROM VEHICLE";
		try {
			if (connection == null)
				connection = dataSource.getConnection();

			final PreparedStatement prepareStatement = connection.prepareStatement(sql);
			prepareStatement.execute();
			prepareStatement.close();
		} catch (final SQLException e) {
			LOG.error("Error executing SQL query", e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public void beginConnection() {
		if (connection == null) {
			try {
				connection = dataSource.getConnection();
			} catch (final SQLException e) {
				LOG.error("Unable to make connection", e);
			}
		}
	}

	@Override
	public void endConnection() {
		if (connection != null) {
			try {
				connection.close();
			} catch (final SQLException e) {
				LOG.error("Unable to close connection", e);
			}
		}

	}
}
